package com.drakulo.games.ais.ui.component;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import com.drakulo.games.ais.AloneInSpace;
import com.drakulo.games.ais.core.GameData;
import com.drakulo.games.ais.ui.I18n;
import com.drakulo.games.ais.ui.component.button.Button;
import com.drakulo.games.ais.ui.component.button.TextButton;
import com.drakulo.games.ais.ui.component.window.CenteredWindow;

/**
 * The game menu component
 * 
 * @author Drakulo
 * 
 */
public class GameMenu extends CenteredWindow {
	/** Options button */
	private Button optionButton;
	/** Load button */
	private Button loadButton;
	/** Save button */
	private Button saveButton;
	/** Exit button */
	private Button exitButton;

	public GameMenu() {
		super(132, 150);

		final int btnSize = 120;
		
		this.optionButton = new TextButton(I18n.get("global.options"));
		this.optionButton.disable();
		this.optionButton.setWidth(btnSize);
		
		this.loadButton = new TextButton(I18n.get("global.load"));
		this.loadButton.disable();
		this.loadButton.setWidth(btnSize);
		
		this.saveButton = new TextButton(I18n.get("global.save"));
		this.saveButton.disable();
		this.saveButton.setWidth(btnSize);
		
		this.exitButton = new TextButton(I18n.get("global.exit"));
		this.exitButton.setWidth(btnSize);

		// Positionning buttons
		int y = this.getOY() + 5;
		final int pitch = Button.DEFAULT_HEIGHT + 10;
		final int x = this.getOX() + this.width / 2 - btnSize / 2;
		this.optionButton.setPosition(x, y);
		y += pitch;

		this.loadButton.setPosition(x, y);
		y += pitch;

		this.saveButton.setPosition(x, y);
		y += pitch;

		this.exitButton.setPosition(x, y);
		y += pitch + Button.DEFAULT_HEIGHT / 2;

		createHandlers();
	}

	@Override
	public void render(Graphics g) throws SlickException {
		super.render(g);

		// Rendering of buttons
		this.optionButton.render(g);
		this.loadButton.render(g);
		this.saveButton.render(g);
		this.exitButton.render(g);
	}

	@Override
	public void update(Input input) throws SlickException {
		super.update(input);
		// Button updates
		this.optionButton.update(input);
		this.loadButton.update(input);
		this.saveButton.update(input);
		this.exitButton.update(input);
	}

	private void createHandlers() {

		this.exitButton.setActionHandler(new ActionHandler() {

			@Override
			public void run() {
				hide();
				GameData.getGame().enterState(AloneInSpace.MAIN_MENU_STATE);
			}
		});
	}

	/**
	 * Show the menu
	 */
	public void show() {
		super.show();
	}
}
